package com.example.dts.controller;

import com.example.dts.controller.base.BaseController;
import com.example.dts.controller.base.OR;
import com.example.dts.model.File;
import com.example.dts.model.ro.FileRO;
import com.example.dts.model.vo.PageVO;
import com.example.dts.service.FileService;
import io.swagger.annotations.Api;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/file")
@Api(value = "文件管理", tags = {"文件管理"})
public class FileController extends BaseController {

    @Resource
    private FileService fileService;

    /**
     * 上传轮播图
     *
     * @param file
     * @return
     */
    @PostMapping("/uploadBanner")
    public ResponseEntity<OR<String>> uploadBanner(@RequestParam MultipartFile file) {
        String userId = currentUser();
        return processData(() -> fileService.uploadBanner(file, userId), this::processException);
    }

    /**
     * 文件分页
     *
     * @param ro
     * @return
     */
    @PostMapping("/page")
    public ResponseEntity<OR<PageVO<File>>> page(@RequestBody FileRO ro) {
        String userId = currentUser();
        return processData(() -> fileService.page(ro, userId), this::processException);
    }

    /**
     * 更新文件
     *
     * @param file
     * @return
     */
    @PostMapping("/update")
    public ResponseEntity<OR<Void>> update(@RequestBody File file) {
        String userId = currentUser();
        fileService.update(file, userId);
        return success();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/del/batch")
    public OR<Void> deleteBatch(@RequestBody List<String> ids) {
        fileService.deleteBatch(ids);
        return successResult();
    }
    /**
     * 根据类型获取所有文件
     *
     * @param type
     * @return
     */
    @GetMapping("/getAllByType")
    public ResponseEntity<OR<List<File>>> getAllByType(@RequestParam String type) {
        return processData(()->fileService.getAllByType(type), this::processException);
    }

}
